package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.FreemarkerService;
import com.heima.common.redis.CacheService;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author jack
 * @data 2024 11:30
 */
@Service
@Transactional
@Slf4j
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;
    @Autowired
    private ApArticleContentMapper apArticleContentMapper;
    @Autowired
    private FreemarkerService freemarkerService;
    @Autowired
    private CacheService cacheService;

    /**
     * app文章加载
     *
     * @param homeDto 筛选条件
     * @param type    控制开关：1 表示加载首页和加载更多  2:表示加载更新
     * @return
     */
    @Override
    public ResponseResult load(ArticleHomeDto homeDto, int type) {
        List<ApArticle> list = apArticleMapper.load(homeDto, type);
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult load1(ArticleHomeDto homeDto, int type) {
        //先查redis获取热点文章
        String hotArticleAll = null;
        if (homeDto.getTag().equals("__all__")) {
            hotArticleAll = cacheService.get("hot_article__all__");
        }else{
            hotArticleAll = cacheService.get("hot_article_"+homeDto.getTag());
        }
        if (hotArticleAll != null) {
            List<ApArticle> apArticleList = JSON.parseArray(hotArticleAll, ApArticle.class);
            return ResponseResult.okResult(apArticleList);
        }

        //redis查不到或者查完了，再去查数据库
        return load(homeDto,type);
    }

    /**
     * 同步自媒体文章到app端
     *
     * @param articleDto
     * @return
     */
    @Override
    public ResponseResult saveOrUpdateArticle(ArticleDto articleDto) {
        ApArticle article = new ApArticle();
        BeanUtils.copyProperties(articleDto, article);

        //1. 判断有没有id
        if (articleDto.getId() == null) {
            //没有：就新增   -- ap_article, ap_article_config, ap_article_content
            apArticleMapper.insert(article);
            articleDto.setId(article.getId());

            ApArticleConfig config = new ApArticleConfig(article.getId());
            apArticleConfigMapper.insert(config);

            ApArticleContent content = new ApArticleContent();
            content.setContent(articleDto.getContent());
            content.setArticleId(article.getId());
            apArticleContentMapper.insert(content);

        } else {
            //有：就修改     -- ap_article, ap_article_content
            apArticleMapper.updateById(article);

            ApArticleContent content = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery()
                    .eq(ApArticleContent::getArticleId, articleDto.getId()));
            content.setContent(articleDto.getContent());
            apArticleContentMapper.updateById(content);
        }

        //生成文章的详情页
        freemarkerService.buildPage(articleDto);

        //2. 将文章id返回给调用者
        return ResponseResult.okResult(String.valueOf(article.getId()));
    }

    /**
     * 文章上下架
     *
     * @param map
     */
    @Override
    public void downOrUpArticle(Map map) {
        //先拿到文章id和enable的值
        String articleId = map.get("articleId").toString();
        Short enable = Short.valueOf(map.get("enable").toString());

        boolean isDown = false;      //默认上架
        if (enable == 0) {
            isDown = true;
        }

        ApArticleConfig config = new ApArticleConfig();
        config.setArticleId(Long.valueOf(articleId));
        config.setIsDown(isDown);
        apArticleConfigMapper.update(config, Wrappers.<ApArticleConfig>lambdaUpdate()
                .set(ApArticleConfig::getIsDown, isDown)
                .eq(ApArticleConfig::getArticleId, articleId));

    }
}
